//
//  Theme.swift
//  ShiZhe
//
//  Created by 陈卿伟 on 16/8/23.
//  Copyright © 2016年 陈卿伟. All rights reserved.
//

import UIKit

public let WidthAPP = UIScreen.mainScreen().bounds.width
public let HeightAPP = UIScreen.mainScreen().bounds.height
public let MainBounds = UIScreen.mainScreen().bounds

var SCALEW:CGFloat = 1
var SCALEH:CGFloat = 1

let iphone4:CGFloat = 480
let iphone5:CGFloat = 568
let iphone6:CGFloat = 667
let iphone6p:CGFloat = 736

let ISLOGIN = "isLogIn"//是否登录的 NSUserDefaults
let USERINFO = "userInfoKey"//存的用户信息



struct Theme {
    //主色调黄色
    static let YellowColor = UIColor(red: 255/255, green: 186/255, blue: 0, alpha: 1)
    //tabbar灰色
    static let BarGrayColor = UIColor(red: 196/255, green: 189/255, blue: 189/255, alpha: 1)
    //主色调灰色
    static let GrayColor = UIColor(red: 147/255, green: 147/255, blue: 147/255, alpha: 1)
    //背景颜色
    static let BGColor = UIColor(red: 245/255, green: 245/255, blue: 245/255, alpha: 1)
    //导航栏上方所有控件文字颜色
    static let NaviColor = UIColor.whiteColor()
    //系统蓝色
    static let BlueColor = UIColor(red: 0/255, green: 65/255, blue: 255/255, alpha: 1)
    //HUD蓝色
    static let BlueHUD = UIColor(red: 0, green: 111/255, blue: 158/255, alpha: 1)
    //编辑的蓝色
    static let editBlue = UIColor(red: 10/255, green: 69/255, blue: 175/255, alpha: 1)
    
    
}

struct Notifiction {
    //注册成功或者登录成功等的通知
    static let LOGIN = "loginNotifiction"
    static let textAttachment = "textAttachment"
}


struct BaseFunc {
    //定义取得字体的方法，默认取得。Heiti SC的字体
    static func getCustomFont(size:CGFloat)->UIFont{
        return UIFont(name: "Heiti SC", size: size*SCALEW)!
    }
    
    
    
    //系统自带的字体
    static func getSystemFont(size:CGFloat)->UIFont{
        return UIFont.boldSystemFontOfSize(size*SCALEW)
    }
    
    //多行细字体
    static func getLightFont(size:CGFloat)->UIFont{
        return UIFont(name: "Heiti SC", size: size*SCALEW)!
    }
    
    //多行粗字体
    static func getPingFangFont(size:CGFloat)->UIFont{
        return UIFont(name: "Heiti SC", size: size*SCALEW)!
    }
    
    //创建一个随机颜色
    static func createARCColor()->UIColor{
        let a = CGFloat(arc4random()%255)
        let b = CGFloat(arc4random()%255)
        let c = CGFloat(arc4random()%255)
        let arcColor = UIColor(red: a/255, green: b/255, blue: c/255, alpha: 1)
        return arcColor
    }
    
    //创建颜色
    static func createCustomColor(r:CGFloat,b:CGFloat,g:CGFloat,a:CGFloat)->UIColor{
        let color = UIColor(red: r/255, green: b/255, blue: g/255, alpha: a)
        return color
    }
    
    //创建placeHolder
    static func showPlaceHolder(view:UIView)->UIView{
        let holder = PlaceholderView(frame: CGRectMake(0,0,view.frame.size.width,view.frame.size.height))
        view.addSubview(holder)
        return holder
    }
    
    //封装计算字体大小的方法
    static func reckonFontSize(text:NSAttributedString,width:CGFloat,fontName:String,fontSize:CGFloat)->CGRect{
        let constraint = CGSize(width: WidthAPP-36*SCALEW, height: CGFloat(MAXFLOAT))
        let size = text.boundingRectWithSize(constraint, options: NSStringDrawingOptions.UsesLineFragmentOrigin, context: nil)
        return size
    }
    
}


//MARK:--HUD
struct showHUD {
    //在上方出现的hud
    static func showTopHud(view:UIView,text:String){
        dispatch_async(dispatch_get_main_queue()) { 
            let hud = TopHud(frame: CGRect(x: 0, y: 0, width: WidthAPP, height: 64), text: text)
            view.window?.addSubview(hud)
        }
    }
    
    //等待的hud
    static func showWaitHud(view:UIView)->UIView{
        let hud = WaitHUD(frame: MainBounds)
        hud.tag = 999
        view.window!.addSubview(hud)
        return hud
    }
    
    static func removeWaitHud(view:UIView){
        let hud = view.viewWithTag(999)
        hud?.removeFromSuperview()
    }
}


extension UIImageView {
    //封装了一下 sdwebImage 和 添加placeholder的方法
    public func setImageForSDWebImage(view:UIImageView,url:NSURL!){
        let placeholder = BaseFunc.showPlaceHolder(view)
        view.sd_setImageWithURL(url) { (ima, error, _, _) in
            if error == nil{
                UIView.animateWithDuration(0.1, animations: {
                    placeholder.removeFromSuperview()
                })
            }else{
                print(error)
            }
        }
    }
}













